<?php
class cmsApi extends baseApi{ 
    //抬头菜单
    public function getHeder(){
        return '<i class="iconfont">&#xF010;</i> 内容管理';
    }
    //首页菜单
    public function getMenu(){
        return model('channel')->leftmenu();
    }
    //APP菜单A
    public function getNav(){
        return array(1 => model('channel')->leftmenu(),2 => array(
            'title'=>'其它内容',
            'list'=>array(
                '<i class="icon">&#xf0c5;</i> 自定义单页'=>url('diypage/lists'),
                '<i class="icon">&#xf016;</i> 网页局部内容'=>url('fragment/lists'),
                '<i class="icon">&#xf0aa;</i> 上传附件'=>url('files/index')),
            ),3 => array(
            'title'=>'栏目管理',
            'list'=>array('<i class="icon">&#xf0e8;</i> 栏目管理'=>url('channel/channel')),
            ),4 => array(
            'title'=>'系统管理',
            'list'=>array(
            '<i class="icon">&#xf00a;</i> 模型表与字段'=>url('module/index'),
            '<i class="icon">&#xf0e8;</i> 多站管理'=>url('channel/language'),
            '<i class="icon">&#xf00b;</i> 栏目管理'=>url('channel/index'),
            '<i class="icon">&#xf029;</i> 资源特性或专题'=>url('special/index'),
            '<i class="icon">&#xf0c5;</i> 自定义单页'=>url('diypage/index'),
            '<i class="icon">&#xf016;</i> 网页局部内容'=>url('fragment/index'),
            '<i class="icon">&#xf02f;</i> 调用标签'=>url('label/index'),
            '<i class="icon">&#xf0ce;</i> 模板管理'=>url('themes/indextpl'),
            '<i class="icon">&#xf013;</i> 上传配置'=>url('files/configs'),
            '<i class="icon">&#xf059;</i> 入门指南'=>url('index/helps')),
            )
        );
    } 
    //CMS模板解析
    public function cmstpl(){
        return array(
            //变量标签
            "/\[([a-zA-Z_]+)\:readurl\]/i"=> "<?php echo api('cms','api_Getaurl',array($$1['id']));?>",//内容链接[list:readurl]
            "/\[([a-zA-Z_]+)\:channelurl\]/i"=> "<?php echo api('cms','api_Getcurl',array($$1['id']));?>",//菜单链接[list:channelurl]
            "/\[([a-zA-Z_]+)\:readsurl\]/i"=> "<?php echo api('cms','api_Getlurl',array($$1['id']));?>",//菜单链接[list:readsurl]
            "/{diypage:(\S+)}/iU"=> "<?php echo api('cms','api_DiyPage',array($1));?>",   //菜单链接[diypage:id]
            "/\[([a-zA-Z_]+)\:([a-zA-Z_]+)\]/iU"=> "<?php echo \$$1['$2']; ?>",  //处理[list:title] 普通标签
            //SQL读取标签,带翻页<!--info:page(标签ID,栏目ID)-->
            "/<!--(\S+):(\S+)\((.+?)\,(.+?)\)-->/i"=> "<?php \$rel=api('cms','api_Lists',array($3,$4,1));$$1 = \$rel[0]; $$2 = \$rel[1];\$$1_i=0;if(!empty($$1)) foreach($$1 as $$1_key=>$$1){\$$1_i++; ?>",
            //SQL读取标签<!--info(标签ID,栏目ID)-->
            "/<!--(\S+)\((.+?)\,(.+?)\)-->/i"=> "<?php $$1=api('cms','api_Lists',array($2,$3));\$$1_i=0;if(!empty($$1)) foreach($$1 as $$1_key=>$$1){\$$1_i++; ?> ",
            //结束SQL标签<!--/info-->
            "/<!--\/([a-zA-Z_]+)-->/i"=> "<?php } unset($$1)?>",
            //替换数据碎片   
            "/{inc:(\S+)}/iU"=> "<?php echo api('cms','api_Fragment',array($1));?>",   
        );
    }
    //标签输入
    public function api_Lists($Label,$ClassID,$Pages = 0){
        $Label   = intval($Label);
        $condition['id'] = $Label;
        $rel = model('label')->find($condition);
        if($rel){
            $rel = postinput($rel,array('title','content','uptime','id'));
            if($rel['es_locks']){ //FALSE根据网址CID切换参数
                $ClassID = intval($ClassID);
            }else{
                $ClassID = intval($_GET['cid']);
            }
            if($rel['type'] == 1){
                return model('channel')->ChannelLabel($rel,$ClassID);
            }elseif($rel['type'] == 2){
                $PageUrl = url('default/index/lists',array('cid'=>$ClassID,'page'=>'{page}'));
                return model('article')->ArticleLabel($rel,$ClassID,$Pages,$PageUrl);
            }else{
                echo 'Error404:SQL标签参数出错!';
            }
        }else{
            echo 'Error404:找不到ID为 '.$Label.' 的标签!';
        }
    }
    //内容链接[list:aurl]
    public function api_Getaurl($fid){
        $condition['id'] = intval($fid);
        $rel = model('article')->find($condition,'id,cid,url');
        if($rel['url']){
           return $rel['url'];
        }else{
           return url('default/index/read',array('cid'=>$rel['cid'],'fid'=>$fid));
        }
    }
    //菜单链接[list:curl]
    public function api_Getcurl($cid){
        $condition['id'] = intval($cid);
        $rel = model('channel')->find($condition,'id,type,url,tpl_channel');
        if($rel['url']){
            return $rel['url'];
        }else{
            if($rel['tpl_channel']){
                return url('default/index/channel',array('cid'=>$cid));
            }else{
                return url('default/index/lists',array('cid'=>$cid));
            } 
        }
    }
    //菜单链接[list:lurl]
    public function api_Getlurl($cid){
        $condition['id'] = intval($cid);
        $rel = model('channel')->find($condition,'id,type,url,tpl_list');
        if($rel['url']){
            return $rel['url'];
        }else{
            if($rel['tpl_list']){
                return url('default/index/lists',array('cid'=>$cid));
            }else{
                return url('default/index/channel',array('cid'=>$cid));
            }
        }
    }
    //菜单链接[list:curl]
    public function api_DiyPage($fid){
        $fid = intval($fid);
        return url('default/index/page',array('uid'=>$fid));
    }
    //替换数据碎片  
    public function api_Fragment($fid){
        $fid = intval($fid);
        return model('fragment')->Write_Fragment($fid);
    }
    //栏目API调用
    //cid栏目id $es_class,查询字段
    public function api_ChannelLabel($cid,$es_class = 'id'){
        return model('channel')->ChannelLabel(array('es_class'=>$es_class),$cid,0);
    }
    //内容API信息 fid内容ID cid内容ID
    public function api_ArticleDisplay($fid,$cid){
        return model('article')->ArticleDisplay($fid,$cid);
    }
    //扩展内容API信息列表  fid内容ID plus_mid扩展模型id
    public function api_ArticleList_Plus($fid,$plus_mid){
        return model('article')->ArticleList_Plus($fid,$plus_mid);
    }
    //扩展内容API信息阅读  fid内容ID plus_mid扩展模型id
    public function api_ArticleDisplay_Plus($fid,$plus_mid){
        return model('article')->ArticleDisplay_Plus($fid,$plus_mid);
    }
    //自定义扩展
    public function api_DiyPageRead($uid){
        return model('diypage')->find('id='.$uid);
    }
}